package com.example.mq.rabbit.config.config;

import com.sun.deploy.association.utility.AppConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitQueueConfig {
    @Bean(name="springMessageQueue")
    public Queue createQueue(@Qualifier("rabbitAdmin")RabbitAdmin rabbitAdmin)
    {
        Queue sendQueue = new Queue(AppConstants.SEND_QUEUE_NAME,true,false,false);
        rabbitAdmin.declareQueue(sendQueue);
        return sendQueue;
    }

    @Bean(name="springMessageExchange")
    public Exchange createExchange(@Qualifier("rabbitAdmin")RabbitAdmin rabbitAdmin)
    {
        DirectExchange sendExchange = new DirectExchange(AppConstants.SEND_EXCHANGE_NAME,true,false);
        rabbitAdmin.declareExchange(sendExchange);
        return sendExchange;
    }

    @Bean(name="springMessageBinding")
    public Binding createMessageBinding(@Qualifier("rabbitAdmin")RabbitAdmin rabbitAdmin)
    {
        Map<String,Object> arguments = new HashMap<String,Object>();
        Binding sendMessageBinding =
                new Binding(AppConstants.SEND_QUEUE_NAME, Binding.DestinationType.QUEUE,
                        AppConstants.SEND_EXCHANGE_NAME, AppConstants.SEND_MESSAGE_KEY, arguments);
        rabbitAdmin.declareBinding(sendMessageBinding);
        return sendMessageBinding;
    }

    @Bean(name="springReplyMessageQueue")
    public Queue createReplyQueue(@Qualifier("rabbitAdmin")RabbitAdmin rabbitAdmin)
    {
        Queue replyQueue = new Queue(AppConstants.REPLY_QUEUE_NAME,true,false,false);
        rabbitAdmin.declareQueue(replyQueue);
        return replyQueue;
    }

    @Bean(name="springReplyMessageExchange")
    public Exchange createReplyExchange(@Qualifier("rabbitAdmin")RabbitAdmin rabbitAdmin)
    {
        DirectExchange replyExchange = new DirectExchange(AppConstants.REPLY_EXCHANGE_NAME,true,false);
        rabbitAdmin.declareExchange(replyExchange);
        return replyExchange;
    }

    @Bean(name="springReplyMessageBinding")
    public Binding createReplyMessageBinding(@Qualifier("rabbitAdmin") RabbitAdmin rabbitAdmin)
    {
        Map<String,Object> arguments = new HashMap<String,Object>();
        Binding replyMessageBinding =
                new Binding(AppConstants.REPLY_QUEUE_NAME, Binding.DestinationType.QUEUE,
                        AppConstants.REPLY_EXCHANGE_NAME, AppConstants.REPLY_MESSAGE_KEY, arguments);
        rabbitAdmin.declareBinding(replyMessageBinding);
        return replyMessageBinding;
    }
}
